/* eslint-disable eqeqeq */
<template>
  <div>
    <x-card>
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户名">
                <a-input v-model="queryParam.username" allow-clear placeholder="请输入用户名"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.is_active" allow-clear placeholder="请选择状态">
                  <a-select-option v-for="(item,index) in statusDictType" :key="index" :value="item.code">{{ item.value
                    }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
      >
        <template slot="operator" v-if="hasPerm('user:add')">
          <a-button @click="$refs.Form.open()" icon="plus" type="primary" v-if="hasPerm('user:add')">新增用户
          </a-button>
        </template>

        <span slot="status" slot-scope="text">
            <a-badge :status="text? 'success':'error'" :text="text? '正常':'禁用'"/>
          </span>

        <span slot="action" slot-scope="text, record">
            <a-popconfirm v-if="hasPerm('user:edit')" placement="topRight" title="确认状态？"
                          @confirm="() => changeUserState(record)">
              <a>{{record.is_active? '禁用':'启用'}}</a>
            </a-popconfirm>
            <a-divider type="vertical" v-if="hasPerm('user:edit')"/>
            <a v-if="hasPerm('user:edit')" @click="$refs.Form.open(record)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('user:edit') & hasPerm('user:delete')"/>
            <a-popconfirm v-if="hasPerm('user:delete')" placement="topRight" title="确认删除？"
                          @confirm="() => sysUserDelete(record)">
              <a style="color: red">删除</a>
            </a-popconfirm>
          </span>
      </s-table>
      <s-form ref="Form" @ok="handleOk"/>
    </a-card>
  </div>
</template>
<script type="text/jsx">
  import { STable, XCard } from '@/components'
  import * as api from '@/api/system/userManage'
  import sForm from './Form'


  const statusDictType = [
    {
      code: 1,
      value: '正常'
    },
    {
      code: 0,
      value: '禁用'
    }
  ]

  export default {
    components: {
      XCard,
      STable,
      sForm
    },
    data() {
      return {
        queryParam: {},
        statusDictType,
        columns: [
          {
            title: '登录名',
            dataIndex: 'username'
          },
          {
            title: '姓名',
            dataIndex: 'nickname'
          },
          {
            title: '角色名',
            dataIndex: 'role_name'
          },
          {
            title: '状态',
            dataIndex: 'is_active',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '最近登录',
            dataIndex: 'last_login'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return api.getPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        }
      }
    },
    created() {
      if (this.hasPerm('user:edit') || this.hasPerm('user:delete')) {
        this.columns.push({
          title: '操作',
          width: '300px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      handleOk() {
        this.$refs.table.refresh()
      },
      // 改变用户当前是否被禁用
      changeUserState(record) {
        const params = { id: record.id, is_active: !record.is_active }
        api.updatePartObj(params).then((res) => {
          if (res.success) {
            this.$message.success('操作成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('操作失败：' + res.message)
          }
        })
      },
      /**
       * 删除用户
       */
      sysUserDelete(record) {
        api.deleteObj(record.id).then((res) => {
          if (!res.error) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.error)
          }
        })
      }
    }
  }
</script>
<style scoped>
  button {
    margin-right: 8px;
  }
</style>
